<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>If only Reeborg could decide on his own ...</title>
</head>
<body>
<h2 class="title">12. <span class="pykeyword">If</span> only Reeborg could decide
on his own ...</h2>
<p><span class="pykeyword">if</span>, <span class=
"pykeyword"><small>if</small></span> ...</p>
<p>Wait a minute! Reeborg can decide make some decisions on his own. Didn't I
tell you?</p>
<!--=================================================-->
<hr width="50%">
<h3 class="section">First decisions</h3>
<p>Well, to tell the truth, Reeborg does need some help to decide: you have
to give him some choice as to what to decide. For example, when Reeborg is right next to a beeper, so that he appears to be "on" a beeper on the screen, you can give him some choices as to what to do: you
could ask him to pick it up as follows</p>
<pre>
<span class="keyword">if</span> on_beeper():
    pick_beeper()
</pre>
<p>Let's look at the meaning of the above code:</p>
<ul>
<li>The python keyword <span class="pykeyword">if</span> tells Reeborg that
some <i>condition</i>, whose value is <tt>True</tt> or <tt>False</tt>, is
going to follow;</li>
<li><tt>on_beeper()</tt> is a condition (or test) which is
<tt>True</tt> if Reeborg is next to a beeper (at the same apparent position
on the screen) and <tt>False</tt> otherwise;
<br/><span class="important">[note that this test was called <tt>next_to_a_beeper()</tt> in previous versions of rur-ple.]</span></li>
<li>The colon (:) precedes the series of instructions that Reeborg must
follow <b>if</b> the condition is <tt>True</tt>;</li>
<li>The series of instructions to follow in that case is indented, just like
we had in the case of definitions.</li>
</ul>
<p>This explanation may seem complicated when you read it, but it's actually
quite simple to use an <span class="pykeyword">if</span> statement. Let's
look at it in a simple example. Suppose we want Reeborg to take 9 steps,
picking up any beepers that are there along the way. (We suppose that there
can be at most one beeper at a given spot.) For example, the starting
position might be like the following:</p>
<img alt="move_pick_start" src="../../images/intro/move_pick_start.png">
<p>and we want the final position to be:</p>
<p><img alt="move_pick_start" src="../../images/intro/move_pick_end.png"></p>
<p>So, we want to ask Reeborg to:</p>
<ul>
<li>Take a step forward;</li>
<li>Check to see if there is a beeper;</li>
<li>Pick up the beeper if there is one; otherwise ignore and keep going;</li>
</ul>
<p>repeating the above steps 9 times. Remember that if we ask Reeborg to pick
up a beeper where there is none, he will complain and turn himself off. Here
is how we can do this:</p>
<pre>
<span class="keyword">def</span> move_and_pick():
    move()
    <span class="keyword">if</span> on_beeper():
        pick_beeper()

repeat(move_and_pick, 9)
turn_off()
</pre>
<p><b>Try it!</b></p>
<hr width="50%">
<!--==================================================-->
<h3 class="try">Harvest time again!</h3>
<p>It's harvest time, yet again! However, this time not all seeds have
sprouted and some carrots are missing. Have Reeborg pick up all the carrots
(represented by beepers) in this garden. The world file is harvest3.wld. Look
back at the second last harvesting exercise that you did last time;
chances are, all that you need to do is change the instruction
<tt>harvest_one_row()</tt> so that it looks similar to the above
<tt>move_and_pick()</tt> instruction; however, it is possible that
you will need one more change. [For example, in my solution to the
harvesting problem, I had defined a <tt>pick_and_move()</tt>
instruction which needed some minor change as well.]</p>
<p><img alt="harvest start" src="../../images/intro/harvest3.png"></p>
<p>Note that your new program should also work <b>as is</b> with the world
file harvest1.wld that we had used before. Try it!</p>
<center><a href="11-repeat.htm"><img alt="previous" src=
"../../images/previous.png"> Avoiding repetitions, again!</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png"></a> - <a href=
"13-else.htm">Listen to me ... or else ....<img alt="next" src=
"../../images/next.png"></a></center>
</body>
</html>
